package org.sjs.dreamfly.biz.news

import android.content.Intent
import android.content.res.Configuration
import android.os.Bundle
import androidx.lifecycle.ViewModelProvider
import com.google.common.eventbus.Subscribe
import kotlinx.coroutines.launch
import org.sjs.dreamfly.R
import org.sjs.dreamfly.app.PlayInfoLoadedEvent
import org.sjs.dreamfly.app.PlayListLoadedEvent
import org.sjs.dreamfly.app.defaultEventBus
import org.sjs.dreamfly.biz.shortvideo.ShortVideoFragment
import org.sjs.dreamfly.browser.BrowserConst
import org.sjs.dreamfly.common.base.BaseActivity
import org.sjs.dreamfly.common.util.LogUtil
import org.sjs.dreamfly.common.util.Router
import org.sjs.dreamfly.model.VideoInfo
import org.sjs.dreamfly.player.PlayerFragment
import org.sjs.dreamfly.player.ShortVideoPlayerFragment

class NewsDetailActivity: BaseActivity() {

    private var viewModel: NewsViewModel? = null
    private var newsFragment: NewsFragment? = null
    private var playerFragment: ShortVideoPlayerFragment? = null

    override fun requireLayoutId(): Int {
        return R.layout.activity_news_detail
    }

    override fun onLoadData(savedInstanceState: Bundle?, newIntent: Intent?) {
        if (newIntent != null) {
            return
        }
        intent.getStringExtra(Router.KEY_URL)?.let {
            if (newsFragment == null) {
                newsFragment = NewsFragment.newInstance(url = it,
                    point = BrowserConst.InjectPoint.POINT_ON_PAGE_FINISH)
                newsFragment!!.useTitleBar(false)
                supportFragmentManager.beginTransaction().replace(R.id.web_view_stub, newsFragment!!).commitAllowingStateLoss()
            } else {
                newsFragment?.loadUrl(it, point = BrowserConst.InjectPoint.POINT_ON_PAGE_FINISH)
            }
        }
    }

    override fun onBindViews() {}

    override fun onInitViews() {}
    override fun onInitViewModel() {
        if (viewModel == null) {
            viewModel = ViewModelProvider(this)[NewsViewModel::class.java]
        }
    }

    override fun onResume() {
        super.onResume()
        defaultEventBus.register(this)
    }

    override fun onPause() {
        super.onPause()
        defaultEventBus.unregister(this)
    }

    override fun onConfigurationChanged(newConfig: Configuration) {
        super.onConfigurationChanged(newConfig)
    }

    @Subscribe
    private fun onEventPlayInfoLoaded(event: PlayInfoLoadedEvent) {
        LogUtil.d("$event")
        mainScope.launch {
            if (viewModel?.onPlayInfoLoaded(event) == true) {
                if (playerFragment == null) {
                    hideLoadingView()

                    hideTitleBar()
                    playerFragment = PlayerFragment.newInstance<ShortVideoPlayerFragment>().apply {
                        this.setDataProvider(viewModel!!)
                    }
                    supportFragmentManager.beginTransaction()
                        .replace(R.id.player_view_stub, playerFragment!!)
                        .commitAllowingStateLoss()
                }
            }
        }
    }

    @Subscribe
    private fun onEventPlayListLoaded(event: PlayListLoadedEvent) {
        LogUtil.d("size:${event.playList.size} $event")
        viewModel?.onPlayListLoaded(event.playList)?.let {
            prepareVideoInfo(it)
        }
    }

    private fun prepareVideoInfo(list: List<VideoInfo>) {
        list.filter { it.refer?.isNotEmpty() == true && it.videoUrl.isNullOrBlank() }.let {
            if (it.isNotEmpty()) {
                newsFragment?.snifferVideoInfo(it)
            }
        }
    }
}